package com.wangijun.yuan.admin.system.controller;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.admin.base.CommonPage;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.admin.system.domain.SysRole;
import com.wangijun.yuan.admin.system.model.dto.UserQryParam;
import com.wangijun.yuan.admin.system.model.dto.UserSaveParam;
import com.wangijun.yuan.admin.system.model.dto.UserUpdateParam;
import com.wangijun.yuan.admin.system.model.vo.SelectVO;
import com.wangijun.yuan.admin.system.model.vo.UserVO;
import com.wangijun.yuan.admin.system.service.SysUserService;
import com.wangijun.yuan.core.model.IdParam;
import com.wangijun.yuan.core.model.IdsParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import java.util.List;

@Tag(name = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private SysUserService userService;
    @Operation(summary = "查", description = "查")
    @PostMapping("/list")
    CommonResult<CommonPage<UserVO>> list(@RequestBody UserQryParam param) {
        return CommonResult.data(userService.getList(param));
    }

    @Operation(summary = "增加", description = "增加")
    @PostMapping("/add")
    CommonResult<Boolean> add(@RequestBody @Validated UserSaveParam param) {
        userService.add(param);
        return CommonResult.data(true);
    }
    @Operation(summary = "ByID获取", description = "ByID获取")
    @PostMapping("/get")
    CommonResult<UserVO> add(@RequestBody @Validated IdParam param) {
        userService.get(param.getId());
        return CommonResult.data(userService.get(param.getId()));
    }

    @Operation(summary = "改", description = "改")
    @PostMapping("/update")
    CommonResult<Boolean> update(@RequestBody @Validated UserUpdateParam param) {
        userService.update(param);
        return CommonResult.data(true);
    }
    @Operation(summary = "删", description = "删")
    @PostMapping("/del")
    CommonResult<Boolean> del(@RequestBody IdParam param) {
        userService.del(param.getId());
        return CommonResult.data(true);
    }
    @Operation(summary = "删", description = "删")
    @PostMapping("/delBatch")
    CommonResult<Boolean> delBatch(@RequestBody IdsParam param) {
        userService.delBatch(param.getIds());
        return CommonResult.data(true);
    }
    @Operation(summary = "重置密码", description = "重置密码")
    @PostMapping("/reset")
    CommonResult<Boolean> reset(@RequestBody IdParam param) {
        userService.reset(param.getId());
        return CommonResult.data(true);
    }


    @Operation(summary = "角色列表", description = "角色列表")
    @GetMapping("/roleList")
    CommonResult<List<SelectVO>> roleList() {
        List<SysRole> list = Db.list(SysRole.class);
        return CommonResult.data(list.stream().map(i -> new SelectVO(i.getName(), i.getId())).toList());
    }
}
